package com.von.platform.impl.hibernate.orm;

import java.sql.Connection;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.vnl.platform.orm.IConnectionManager;


public class HibernateStandaloneCM implements IConnectionManager {

    private String configureFile;

    private static final ThreadLocal<Session> currentSession = new ThreadLocal<Session>();
    
    public HibernateStandaloneCM() {
        this.configureFile = "/hibernate.cfg.xml";
    }
    
    @Override
    public Connection getCurrentConnection() {
        throw new RuntimeException("Hibernate Standalone Connection Manager does not support this type of connection");
    }

    @Override
    public Object getCurrentSession() {
        if (currentSession.get() == null) {
            Session session = new Configuration().configure(configureFile).buildSessionFactory().openSession();
            currentSession.set(session);
        }
        return currentSession.get();
    }

    public String getConfigureFile() {
        return configureFile;
    }

    public void setConfigureFile(String configureFile) {
        this.configureFile = configureFile;
    }

}
